<?php

declare(ENCODING = 'UTF-8');

/*                                                                        *
 * This script is part of the TYPO3 project - inspiring people to share!  *
 *                                                                        *
 * TYPO3 is free software; you can redistribute it and/or modify it under *
 * the terms of the GNU General Public License version 2 as published by  *
 * the Free Software Foundation.                                          *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *	
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
 * Public License for more details.                                       *
 *                                                                        */	

require_once(t3lib_extMgm::extPath('auxo') . 'core/class.tx_auxo_loader.php');

/**
 * @package auxo
 * @subpackage tests
 * @version $Id$
 */

/**	
 * Logging Testsuite 
 *	
 * @package auxo
 * @subpackage tests	
 * @version $Id$	
 * @copyright Copyright belongs to the respective authors
 * @author AHN
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
 */
class  tx_auxo_Logger_testcase extends PHPUnit_Framework_testcase {
	/**
	 * A Class Loader instance
	 *
	 * @var tx_auxo_loader
	 */
	private $loader = NULL;
	
	/**
	 * A Logger instance
	 * 
	 * @var tx_auxo_Logger
	 */
	/**
	 * Sets up a test enviroment of this testcase.
	 * 
	 * @return void
	 */
	public function setUp() {
		@unlink(dirname(__FILE__) . '/fixtures/fixture.log');
		$this->loader = new tx_auxo_loader();
		$this->logger = new tx_auxo_logger();
		$this->logger->setLevel(tx_auxo_logger::ALL);
		$this->appender = new tx_auxo_LogFileAppender(dirname(__FILE__) . '/fixtures/fixture.log');
		$this->logger->setAppender($this->appender);
		$layout = new tx_auxo_LogDefaultLayout();
		$this->appender->setLayout($layout);
	}
	
	/**
	 * Testcase: Logger with File Appender Logs an Information
	 * 
	 * @return void
	 */
	public function testLoggerWithFileAppenderLogsAnInformation() {
		$this->assertTrue($this->logger->info('This is an information'));
		$this->assertRegexp('/^[0-9]+\/[0-9]+\/[0-9]+ [0-9]+:[0-9]+ \[Info\]: This is an information$/', file_get_contents($this->appender->getFilename()));		
	}

	/**
	 * Testcase: Logger with File Appender Logs a Warning
	 * 
	 * @return void
	 */
	public function testLoggerWithFileAppenderLogsAWarning() {	
		$this->assertTrue($this->logger->warning('This is a warning'));
		$this->assertRegexp('/^[0-9]+\/[0-9]+\/[0-9]+ [0-9]+:[0-9]+ \[Warning\]: This is a warning$/', file_get_contents($this->appender->getFilename()));		
	}

	/**
	 * Testcase: Logger with File Appender Logs an Error
	 * 
	 * @return void
	 */
	public function testLoggerWithFileAppenderLogsAnError() {	
		$this->assertTrue($this->logger->error('This is an error'));
		$this->assertRegexp('/^[0-9]+\/[0-9]+\/[0-9]+ [0-9]+:[0-9]+ \[Error\]: This is an error$/', file_get_contents($this->appender->getFilename()));		
	}

	/**
	 * Testcase: Logger with File Appender Logs a Fatal Error
	 * 
	 * @return void
	 */
	public function testLoggerWithFileAppenderLogsAFatalError() {		
		$this->assertTrue($this->logger->fatal('This is a fatal error'));
		$this->assertRegexp('/^[0-9]+\/[0-9]+\/[0-9]+ [0-9]+:[0-9]+ \[Fatal\]: This is a fatal error$/', file_get_contents($this->appender->getFilename()));		
	}	

	/**
	 * Testcase: Logger with File Appender Logs a Trace
	 * 
	 * @return void
	 */
	public function testLoggerWithFileAppenderLogsATrace() {	
		$this->assertTrue($this->logger->trace('This is a trace'));
		$this->assertRegexp('/^[0-9]+\/[0-9]+\/[0-9]+ [0-9]+:[0-9]+ \[Trace\]: This is a trace$/', file_get_contents($this->appender->getFilename()));		
	}	

	/**
	 * Testcase: Logger with File Appender Logs a Trace
	 * 
	 * @return void
	 */
	public function testLoggerWithFileAppenderLogsADebug() {	
		$value = 12345.12;
		$this->assertTrue($this->logger->debug($value));
		$this->assertRegexp('/^[0-9]+\/[0-9]+\/[0-9]+ [0-9]+:[0-9]+ \[Debug\]: float\(12345\.12\)$/', file_get_contents($this->appender->getFilename()));		
	}
		
	/**
	 * Tears down a created test enviroment of this testcase.
	 */
	public function tearDown() {
		$this->logger->close();
		@unlink($this->appender->getFilename());
		unset($this->logger);		
	}
}